
using System;
using System.Collections.Generic;
using System.Text;
using ScrewTurn.Wiki.PluginFramework;
using MySql.Data.MySqlClient;

namespace ScrewTurn.Wiki.PluginPack {

	/// <summary>
	/// Implements a base class for MySQL Providers.
	/// </summary>
	[Serializable]
	public abstract class MySqlStorageProviderBase {

		protected string config;
		protected IHost host;

		public void Init(IHost host, string config) {
			this.host = host;
			this.config = config;
			if(!ValidateConfig()) throw new InvalidConfigurationException("Unable to perform the Test Query, check the Configuration String and the Database.");
			if(!IsDatabaseUpToDate()) {
				if(!UpdateDatabase()) throw new Exception("Unable to update the Database Schema.");
			}
		}

		protected abstract bool ValidateConfig();

		protected abstract bool IsDatabaseUpToDate();

		protected abstract bool UpdateDatabase();

		/// <summary>
		/// Gets a new SQL Connection.
		/// </summary>
		/// <returns>The SQL Connection.</returns>
		protected MySqlConnection GetConnection() {
			return new MySqlConnection(config);
		}

		/// <summary>
		/// Gets a new SQL Command, set with a new SQL Connection.
		/// </summary>
		/// <returns>The SQL Command.</returns>
		protected MySqlCommand GetCommand() {
			return GetConnection().CreateCommand();
		}

		/// <summary>
		/// Closes the Connection used by a SQL Command.
		/// </summary>
		/// <param name="cmd">The SQL Command.</param>
		protected void Close(MySqlCommand cmd) {
			try {
				cmd.Connection.Close();
			}
			catch { }
		}

		/// <summary>
		/// Executes the method <b>ExecuteScalar</b> of a SQL Command and returns the result.
		/// </summary>
		/// <param name="cmd">The SQL Command.</param>
		/// <returns>The result, or null.</returns>
		/// <remarks>The method automatically opens and then closes the connection.</remarks>
		protected object ExecuteScalar(MySqlCommand cmd) {
			object result = null;
			try {
				cmd.Connection.Open();
				result = cmd.ExecuteScalar();
			}
			catch(Exception ex) {
				result = null;
				host.LogEntry(ex.Message, LogEntryType.Error, this);
			}
			Close(cmd);
			return result;
		}

		/// <summary>
		/// Executes the method <b>ExecuteReader</b> of a SQL Command and returns the SQL Data Reader.
		/// </summary>
		/// <param name="cmd">The SQL Command.</param>
		/// <returns>The SQL Data Reader, or null.</returns>
		/// <remarks>The method automatically opens the connection but it <b>does not</b> closes it.</remarks>
		protected MySqlDataReader ExecuteReader(MySqlCommand cmd) {
			MySqlDataReader result = null;
			try {
				cmd.Connection.Open();
				result = cmd.ExecuteReader();
			}
			catch(Exception ex) {
				host.LogEntry(ex.Message, LogEntryType.Error, this);
				result = null;
			}
			return result;
		}

		/// <summary>
		/// Executes the method <b>ExecuteNonQuery</b> of a SQL Command and returns the result.
		/// </summary>
		/// <param name="cmd">The SQL Command.</param>
		/// <returns>The result, or -1.</returns>
		/// <remarks>The method automatically opens and then closes the connection.</remarks>
		protected int ExecuteNonQuery(MySqlCommand cmd) {
			int result = -1;
			try {
				cmd.Connection.Open();
				result = cmd.ExecuteNonQuery();
			}
			catch(Exception ex) {
				host.LogEntry(ex.Message, LogEntryType.Error, this);
				result = -1;
			}
			Close(cmd);
			return result;
		}

		public void Shutdown() { }

	}

}
